library(readstata13)

# Set the working directory
setwd("/Users/feiyasuo/Documents/RA/racial/final")

# Read data
anes_1986_2012_raw <- read.dta13("anes_timeseries_cdf.dta")
anes_2016_raw <- read.dta13("anes_timeseries_2016.dta")

# Keep the variables we need
anes_1986_2012_raw <- anes_1986_2012_raw[anes_1986_2012_raw$VCF0004>=1986,]
anes_1986_2012 <- data.frame(id=anes_1986_2012_raw$VCF0006a, year=anes_1986_2012_raw$VCF0004, 
                             state=anes_1986_2012_raw$VCF0901b, age=anes_1986_2012_raw$VCF0101, 
                             gender=anes_1986_2012_raw$VCF0104, highest_education=anes_1986_2012_raw$VCF0110, 
                             race=anes_1986_2012_raw$VCF0106, special_favor=anes_1986_2012_raw$VCF9040, 
                             slavery=anes_1986_2012_raw$VCF9039, deserve=anes_1986_2012_raw$VCF9042, 
                             try_harder=anes_1986_2012_raw$VCF9041, stringsAsFactors=FALSE)

anes_1986_2012 <- anes_1986_2012[anes_1986_2012$year!=1996,]
anes_1986_2012 <- anes_1986_2012[anes_1986_2012$year!=1998,]
anes_1986_2012 <- anes_1986_2012[anes_1986_2012$year!=2002,]
anes_1986_2012 <- anes_1986_2012[anes_1986_2012$year!=2006,]
anes_1986_2012 <- anes_1986_2012[anes_1986_2012$year!=2010,]

anes_2016 <- data.frame(id=anes_2016_raw$V160001, year=2016, state=anes_2016_raw$V161010e, age=anes_2016_raw$V161267, 
                        gender=anes_2016_raw$V161342, highest_education=anes_2016_raw$V161270, 
                        race=anes_2016_raw$V161310x, special_favor=anes_2016_raw$V162211, slavery=anes_2016_raw$V162212,
                        deserve=anes_2016_raw$V162213, try_harder=anes_2016_raw$V162214, stringsAsFactors=FALSE)

# Recode variables
## ones do not need change: id=respondent id, year=survey year 
### state: state=respondent's state(2-character), otherwise NA
anes_1986_2012$state[anes_1986_2012$state==99]<-NA

### age=respondent's age (1992: 91=91 or older; 2012: 90=90 or older; 1996 and later:97=97 years old or older; 1976-1990,1994,2002: 99=99 years oldor older)
anes_1986_2012$age[anes_1986_2012$age<=0]<-NA
anes_2016$age[anes_2016$age<=0]<-NA

### set up age group: 1=18-29, 2=30-44, 3=45-64, 4=65+, otherwise NA
anes_2016$age_group[anes_2016$age>=18 & anes_2016$age <=29] <- 1
anes_2016$age_group[anes_2016$age>=30 & anes_2016$age <=44] <- 2
anes_2016$age_group[anes_2016$age>=45 & anes_2016$age <=64] <- 3
anes_2016$age_group[anes_2016$age>=65] <- 4

anes_1986_2012$age_group[anes_1986_2012$age>=18 & anes_1986_2012$age <=29] <- 1
anes_1986_2012$age_group[anes_1986_2012$age>=30 & anes_1986_2012$age <=44] <- 2
anes_1986_2012$age_group[anes_1986_2012$age>=45 & anes_1986_2012$age <=64] <- 3
anes_1986_2012$age_group[anes_1986_2012$age>=65] <- 4

### gender:if women=1, if man=0, if other=2
anes_1986_2012$gender <- ifelse(anes_1986_2012$gender=="2. Female",1,0)
anes_2016$gender <- ifelse(anes_2016$gender<0, NA, anes_2016$gender-1)

### highest_education: 1=Grade school or less (0-8 grades), 2=High school (12 grades or fewer, incl. non-college, 3=Some college (13 grades or more but no degree, 4=College or advanced degree
anes_1986_2012$highest_education<-as.character(anes_1986_2012$highest_education)
anes_1986_2012$highest_education[anes_1986_2012$highest_education==
                                   "0. DK; NA; no Pre IW; short-form 'new' Cross Section"] <- NA
anes_1986_2012$highest_education[anes_1986_2012$highest_education==
                                   "1. Grade school or less (0-8 grades)"] <- 1
anes_1986_2012$highest_education[anes_1986_2012$highest_education==
                                             "2. High school (12 grades or fewer, incl. non-college"]<-2 
anes_1986_2012$highest_education[anes_1986_2012$highest_education==
                                   "3. Some college (13 grades or more but no degree;"]<-3
anes_1986_2012$highest_education[anes_1986_2012$highest_education==
                                   "4. College or advanced degree (no cases 1948)"]<-4

anes_2016$highest_education[anes_2016$highest_education<0|anes_2016$highest_education==95] <- NA
anes_2016$highest_education[anes_2016$highest_education==4] <- 1
anes_2016$highest_education[anes_2016$highest_education>=5 & anes_2016$highest_education <= 9] <- 2
anes_2016$highest_education[anes_2016$highest_education>=10 & anes_2016$highest_education<=12] <- 3
anes_2016$highest_education[anes_2016$highest_education>=13] <- 4

### race: 1=White non-Hispanic, 2=Black non-Hispanic, 3=Other
anes_1986_2012$race <- as.character(anes_1986_2012$race)
anes_1986_2012$race[anes_1986_2012$race=="0. Missing, pre-1966 data" | anes_1986_2012$race=="9. Missing, DK/REF/NA"] <- NA
anes_1986_2012$race[anes_1986_2012$race=="1. White non-Hispanic"] <- 1
anes_1986_2012$race[anes_1986_2012$race=="2. Black non-Hispanic"] <- 2
anes_1986_2012$race[anes_1986_2012$race=="3. Other"] <- 3

anes_2016$race[anes_2016$race<0] <- NA
anes_2016$race[anes_2016$race>=3] <- 3

### special_favor:1=most liberal(strongly disagree), 5=most conservative(strongly agree)
anes_1986_2012$special_favor <- as.character(anes_1986_2012$special_favor)
  
anes_1986_2012$special_favor[anes_1986_2012$special_favor=="9. NA; no Post IW; form A (1986); form B (1990)"] <- NA
anes_1986_2012$special_favor[anes_1986_2012$special_favor=="1. Agree strongly"] <- 5
anes_1986_2012$special_favor[anes_1986_2012$special_favor=="2. Agree somewhat"] <- 4
anes_1986_2012$special_favor[anes_1986_2012$special_favor=="3. Neither agree nor disagree"] <- 3
anes_1986_2012$special_favor[anes_1986_2012$special_favor=="4. Disagree somewhat"] <- 2
anes_1986_2012$special_favor[anes_1986_2012$special_favor=="5. Disagree strongly"] <- 1
anes_1986_2012$special_favor[anes_1986_2012$special_favor=="8. DK"] <- 3


anes_2016$special_favor <- anes_2016$special_favor * 10
anes_2016$special_favor[anes_2016$special_favor==-80] <- 3
anes_2016$special_favor[anes_2016$special_favor==-90] <- 3
anes_2016$special_favor[anes_2016$special_favor==50] <- 1
anes_2016$special_favor[anes_2016$special_favor==40] <- 2
anes_2016$special_favor[anes_2016$special_favor==30] <- 3
anes_2016$special_favor[anes_2016$special_favor==20] <- 4
anes_2016$special_favor[anes_2016$special_favor==10] <- 5
anes_2016$special_favor[anes_2016$special_favor<0] <- NA

### slavery: 1=most liberal(strongly agree), 5=most conservative(strongly disagree)
anes_1986_2012$slavery <- as.character(anes_1986_2012$slavery)

anes_1986_2012$slavery[anes_1986_2012$slavery=="1. Agree strongly"] <- 1
anes_1986_2012$slavery[anes_1986_2012$slavery=="2. Agree somewhat"] <- 2
anes_1986_2012$slavery[anes_1986_2012$slavery=="3. Neither agree nor disagree"] <- 3
anes_1986_2012$slavery[anes_1986_2012$slavery=="4. Disagree somewhat"] <- 4
anes_1986_2012$slavery[anes_1986_2012$slavery=="5. Disagree strongly"] <- 5
anes_1986_2012$slavery[anes_1986_2012$slavery=="9. NA; no Post IW; form A (1986); form B (1990)"] <- NA
anes_1986_2012$slavery[anes_1986_2012$slavery=="8. DK"] <- 3

anes_2016$slavery[anes_2016$slavery==-8 | anes_2016$slavery==-9] <- 3
anes_2016$slavery[anes_2016$slavery<0] <- NA

### deserve: 1=most liberal(strongly agree), 5=most conservative(strongly disagree)
anes_1986_2012$deserve <- as.character(anes_1986_2012$deserve)

anes_1986_2012$deserve[anes_1986_2012$deserve=="1. Agree strongly"] <- 1
anes_1986_2012$deserve[anes_1986_2012$deserve=="2. Agree somewhat"] <- 2
anes_1986_2012$deserve[anes_1986_2012$deserve=="3. Neither agree nor disagree"] <- 3
anes_1986_2012$deserve[anes_1986_2012$deserve=="4. Disagree somewhat"] <- 4
anes_1986_2012$deserve[anes_1986_2012$deserve=="5. Disagree strongly"] <- 5
anes_1986_2012$deserve[anes_1986_2012$deserve=="8. DK"] <- 3
anes_1986_2012$deserve[anes_1986_2012$deserve=="9. NA; no Post IW; form A (1986); form B (1990)"] <- NA

anes_2016$deserve[anes_2016$deserve==-8 | anes_2016$deserve==-9] <- 3
anes_2016$deserve[anes_2016$deserve<0] <- NA

### try_harder: 1=most liberal(strongly disagree), 5=most conservative(strongly agree)
anes_1986_2012$try_harder <- as.character(anes_1986_2012$try_harder)

anes_1986_2012$try_harder[anes_1986_2012$try_harder=="1. Agree strongly"] <- 5
anes_1986_2012$try_harder[anes_1986_2012$try_harder=="2. Agree somewhat"] <- 4
anes_1986_2012$try_harder[anes_1986_2012$try_harder=="3. Neither agree nor disagree"] <- 3
anes_1986_2012$try_harder[anes_1986_2012$try_harder=="4. Disagree somewhat"] <- 2
anes_1986_2012$try_harder[anes_1986_2012$try_harder=="5. Disagree strongly"] <- 1
anes_1986_2012$try_harder[anes_1986_2012$try_harder=="8. DK"] <- 3
anes_1986_2012$try_harder[anes_1986_2012$try_harder=="9. NA; no Post IW; form A (1986); form B (1990)"] <- NA

anes_2016$try_harder <- anes_2016$try_harder * 10
anes_2016$try_harder[anes_2016$try_harder==-80 | anes_2016$try_harder==-90] <- 3
anes_2016$try_harder[anes_2016$try_harder==10] <- 5
anes_2016$try_harder[anes_2016$try_harder==20] <- 4
anes_2016$try_harder[anes_2016$try_harder==30] <- 3
anes_2016$try_harder[anes_2016$try_harder==40] <- 2
anes_2016$try_harder[anes_2016$try_harder==50] <- 1
anes_2016$try_harder[anes_2016$try_harder<0] <- NA

# Merge data
anes <- rbind.data.frame(anes_1986_2012, anes_2016)
anes <- anes[!is.na(anes$try_harder),]
anes <- anes[!is.na(anes$special_favor),]
anes <- anes[!is.na(anes$deserve),]
anes <- anes[!is.na(anes$slavery),]

# Create index for racial resentment
anes$special_favor <- as.numeric(anes$special_favor)
anes$slavery <- as.numeric(anes$slavery)
anes$deserve <- as.numeric(anes$deserve)
anes$try_harder <- as.numeric(anes$try_harder)
anes$racial_index <- anes$special_favor + anes$slavery + anes$deserve + anes$try_harder

# Create interactions
### race and gender (six binary variables)
anes$age_group <- as.factor(anes$age_group)
anes$gender[anes$gender==2] <- NA
anes$gender[anes$gender==1] <- "female"
anes$gender[anes$gender==0] <- "male"
anes$gender <- as.factor(anes$gender)
anes$gender <- relevel(anes$gender, "male")

anes$race[anes$race==1] <- "White"
anes$race[anes$race==2] <- "Black"
anes$race[anes$race==3] <- "Other"
anes$race <- as.factor(anes$race)
anes$race <- relevel(anes$race, "White")

anes$white_male <- ifelse(anes$race=="White" & anes$gender=="male",1,0)
anes$white_female <- ifelse(anes$race=="White" & anes$gender=="female",1,0)
anes$black_male <- ifelse(anes$race=="Black" & anes$gender=="male",1,0)
anes$black_female <- ifelse(anes$race=="Black" & anes$gender=="female",1,0)
anes$other_male <- ifelse(anes$race=="Other" & anes$gender=="male",1,0)
anes$other_female <- ifelse(anes$race=="Other" & anes$gender=="female",1,0)


# Adding state ideology to opinion and census data
ideology <- read.csv("ideology.csv")

anes <- anes[!is.na(anes$state),]
anes$ideology = NA

for (i in 1:nrow(anes)) {
  if (length(ideology$measure[ideology$state==anes$state[i] & ideology$year==anes$year[i]])==1)
    anes$ideology[i] <- 
      ideology$measure[ideology$state==anes$state[i] & ideology$year==anes$year[i]]
}

# Save data
anes<-anes[anes$year!=1986,]
write.csv(anes,"anes_final.csv")
